<template>
  <!--导航模块开始-->
  <nav class="nav">
    <div class="heart">
      <!--全部分类菜单模块开始-->
      <div class="dropdown">
        <div class="dt" @mouseenter="entershow" @mouseleave="leaveshow">
          全部商品分类
        </div>
        <!--过渡动画-->
        <transition name="dds">
          <div
            class="dd"
            v-show="show"
            @mouseenter="entershow"
            @mouseleave="leaveshow"
          >
            <ul @click="goSearch">
              <li
                v-for="clo1 in categoryList.slice(0, 15)"
                :key="clo1.categoryId"
              >
                <a
                  :data-categoryName="clo1.categoryName"
                  :data-category1Id="clo1.categoryId"
                  >{{ clo1.categoryName }}</a
                >
                <div class="item">
                  <dl
                    class="item-row"
                    v-for="clo2 in clo1.categoryChild"
                    :key="clo2.categoryId"
                  >
                    <dt>
                      <a
                        :data-categoryName="clo2.categoryName"
                        :data-category2Id="clo2.categoryId"
                        >{{ clo2.categoryName }}</a
                      >
                    </dt>
                    <div class="dds">
                      <dd
                        v-for="clo3 in clo2.categoryChild"
                        :key="clo3.categoryId"
                      >
                        <a
                          :data-categoryName="clo3.categoryName"
                          :data-category3Id="clo3.categoryId"
                          >{{ clo3.categoryName }}</a
                        >
                      </dd>
                    </div>
                  </dl>
                </div>
              </li>
            </ul>
          </div>
        </transition>
      </div>
      <!--全部分类菜单模块结束-->
      <!--导航各个项目开始-->
      <div class="navitems">
        <ul>
          <li><a href="#">限时秒杀</a></li>
          <li><a href="#">优惠券</a></li>
          <li><a href="#">服装城</a></li>
          <li><a href="#">品牌闪购</a></li>
          <li><a href="#">拍卖</a></li>
          <li><a href="#">京东家电</a></li>
          <li><a href="#">京东超市</a></li>
          <li><a href="#">全球购</a></li>
        </ul>
      </div>
      <!--导航各个项目结束-->
    </div>
  </nav>
  <!--导航模块结束-->
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "TypeNav",
  data() {
    return {
      show: true,
    };
  },
  mounted() {
    if (this.$route.path != "/home") {
      this.show = false;
    }
  },
  computed: {
    ...mapState({
      categoryList: (state) => state.home.categoryList,
    }),
  },
  methods: {
    goSearch(event) {
      let element = event.target;
      let { categoryname, category1id, category2id, category3id } =
        element.dataset;

      if (categoryname) {
        let location = { name: "search" };
        let query = { categoryName: categoryname };
        if (category1id) {
          query.category1Id = category1id;
        } else if (category2id) {
          query.category2Id = category2id;
        } else {
          query.category3Id = category3id;
        }
        //如果路由带params参数也给加上
        if (this.$route.params) {
          location.params = this.$route.params;
          location.query = query;
          this.$router.push(location);
        }
      }
    },
    //当鼠标进入时让商品列表分类展示
    entershow() {
      this.show = true;
    },
    //当鼠标进入时让商品列表分类隐藏
    leaveshow() {
      if (this.$route.path != "/home") {
        this.show = false;
      }
    },
  },
};
</script>

<style lang="css" scoped>
/*导航栏模块*/
.nav {
  width: 100%;
  height: 47px;
  border-top: 1px #ccc solid;
}
.nav .dropdown {
  float: left;
  width: 210px;
  height: 45px;
  background-color: #e2231a;
}
.nav .navitems {
  float: left;
}
/*全部商品分类*/
.dropdown .dt {
  width: 100%;
  height: 100%;
  color: white;
  text-align: center;
  line-height: 45px;
  font-size: 16px;
}
.dropdown .dd {
  position: relative;
  width: 100%;
  height: 461px;
  background-color: #fafafa;
  z-index: 99;
}
.dropdown .dds-enter,
.dropdown .dds-leave-to {
  opacity: 0;
}
.dropdown .dds-enter-to,
.dropdown .dds-leave {
  opacity: 100%;
}
.dropdown .dds-enter-active,
.dropdown .dds-leave-active {
  transition: all 0.2s linear;
}
.dropdown .dd ul li {
  height: 30px;
  line-height: 30px;
  margin-left: 2px;
  padding-left: 12px;
  font-size: 14px;
  color: #333;
}
.dropdown .dd ul li:hover {
  background-color: #d9d9d9;
}
.dropdown .dd ul li a:hover {
  color: #e2231a;
}
.dropdown .dd ul li::after {
  float: right;
  margin-right: 5px;
  font-family: "iconfont";
  font-size: 8px;
  content: "\e8d2";
}
.dropdown .item {
  display: none;
  position: absolute;
  width: 734px;
  min-height: 461px;
  background: #f7f7f7;
  left: 210px;
  top: 0;
  padding: 8px 0;
  border: 1px solid #ddd;
  z-index: 9;
}
.dropdown .dd ul li:hover .item {
  display: block;
}
.item .item-row {
  float: left;
  width: 680px;
  min-height: 37px;
  margin: 6px 20px;
  line-height: 37px;
  font-size: 12px;
}
.item-row dt {
  position: relative;
  float: left;
  min-width: 60px;
  height: 100%;
  font-weight: 700;
  text-align: right;
  padding-right: 8px;
  color: #666;
}
.item-row dt::after {
  position: absolute;
  content: "|";
  font-weight: 400;
  right: 0;
  color: #ccc;
}
.item-row dd {
  float: left;
  padding-left: 12px;
}
.item-row .dds {
  float: left;
  max-width: 600px;
  height: auto;
}
.dd a:hover {
  cursor: pointer;
}
/*导航各个项目*/
.navitems ul li {
  float: left;
}
.navitems ul li a {
  display: block;
  height: 47px;
  line-height: 45px;
  text-align: center;
  font-size: 16px;
  padding: 0 25px;
}
</style>
